import numpy as np
import yaml
from models import BPNetwork
from utils.data_loader import load_mnist, preprocess_data
from sklearn.metrics import classification_report, confusion_matrix

def load_model(model_path='models/saved/model_weights.npz'):
    data = np.load(model_path)
    model = BPNetwork(784, 100, 10)  # 临时创建对象
    model.W1 = data['W1']
    model.b1 = data['b1']
    model.W2 = data['W2']
    model.b2 = data['b2']
    return model

def main():
    # 加载配置
    with open('configs/hyperparams.yaml', 'r') as f:
        config = yaml.safe_load(f)
    
    # 加载数据
    X, y = load_mnist()
    _, X_test, _, y_test = preprocess_data(
        X, y, test_size=config['test_size'])
    
    # 加载模型
    model = load_model()
    
    # 评估模型
    y_pred = model.predict(X_test)
    y_true = np.argmax(y_test, axis=1)
    
    # 计算准确率
    accuracy = np.mean(y_pred == y_true)
    print(f"Test Accuracy: {accuracy:.4f}")
    
    # 分类报告
    print("\nClassification Report:")
    print(classification_report(y_true, y_pred))
    
    # 混淆矩阵
    print("\nConfusion Matrix:")
    print(confusion_matrix(y_true, y_pred))

if __name__ == '__main__':
    main()